package com.checker.designPattern.creation.singleTon;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

public class SigletonTest {
    public static void main(String[] args) throws InterruptedException {

        ConcurrentHashMap<Integer, HungrySingleton> map = new ConcurrentHashMap<>();
//        ConcurrentHashMap<Integer, DCL> map = new ConcurrentHashMap<>();
//        ConcurrentHashMap<Integer, StaticInner> map = new ConcurrentHashMap<>();
//        ConcurrentHashMap<Integer, EnumSingleton> map = new ConcurrentHashMap<>();

        CountDownLatch latch = new CountDownLatch(1000);

        for (int i = 0; i < 1000; i++) {
            int t = i;
            new Thread(
                    () -> {

                        HungrySingleton sigleton = HungrySingleton.getInstance();
//                        DCL sigleton = DCL.getInstance();
//                        StaticInner sigleton = StaticInner.getInstance();
//                        EnumSingleton sigleton = EnumSingleton.INSTANCE;
                        map.put(sigleton.hashCode(), sigleton);
                        System.out.println(t + ":" + sigleton.hashCode());
                        latch.countDown();
                    }
            ).start();
        }


        latch.await();
        System.out.println("======");

        for (Map.Entry<Integer, HungrySingleton> entry : map.entrySet()) {
//        for (Map.Entry<Integer, DCL> entry : map.entrySet()) {
//        for (Map.Entry<Integer, StaticInner> entry : map.entrySet()) {
//        for (Map.Entry<Integer, EnumSingleton> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        System.out.println("map中的元素数量："+map.size()+",是否单例："+(map.size() == 1));
    }
}
